package ro.imc.andrei.visual;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.Border;

import ro.imc.andrei.beans.Course;
import ro.imc.andrei.beans.Professor;
import ro.imc.andrei.beans.Student;
import ro.imc.andrei.ums.UMSDB;

public class MainFrame extends JFrame{
	
	//Panel
	private JPanel mainPanel = new JPanel();
	private JPanel searchPanel = new JPanel();
	private JPanel addPanel = new JPanel();
	private JPanel searchResultsPanel = new JPanel();
	private JPanel exitButtonPanel = new JPanel();
	
	//Text Fields
	private JTextField searchTextField = new JTextField();
	
	//Labels
	private JLabel searchLabel = new JLabel("Search");
	private JLabel addLabel = new JLabel("Add");
	
	//Combo Boxes
	private JComboBox<String> searchComboBox = new JComboBox<String>();
	private JComboBox<String> addComboBox = new JComboBox<String>();
	
	//Buttons
	private JButton searchButton = new JButton("Search");
	private JButton addButton = new JButton("Add");
	private JButton exitButton = new JButton("Exit");
	
	//Table
	private JTable searchTable;
	
	//Scroll Pane
	private JScrollPane tableScrollPane = new JScrollPane();
	
	//Static Variables
	private static int HEIGTH = 600;
	private static int WIDTH = 850;
	private static int PANEL_WIDTH = 800;
	
	public MainFrame(){
		initFrame();
	}
	
	private void initFrame(){
		//Init Combo Boxes - Add Combo Box
		addComboBox.removeAllItems();
		addComboBox.addItem("Student");
		addComboBox.addItem("Professor");
		addComboBox.addItem("Group");
		addComboBox.addItem("Course");
		
		//Init Combo Boxes - Search Combo Box
		searchComboBox.removeAllItems();
		searchComboBox.addItem("Student");
		searchComboBox.addItem("Professor");
		searchComboBox.addItem("Group");
		searchComboBox.addItem("Course");
		
		//Init Frame Details
		//this.setLocationRelativeTo(null);
		this.setSize(WIDTH, HEIGTH);
		//this.setResizable(false);
		
		this.add(mainPanel);
		//jPanelMainPanel.setLayout(new GridLayout(0,1));
		mainPanel.setLayout(null);
		
		//Prepare Search Panel
		searchPanel.setLayout(null);
		searchPanel.setBounds(0, 20, PANEL_WIDTH, 50);
		
		//Set components bounds for Search Panel
		searchLabel.setBounds(10, 10, 50, 25);
		searchTextField.setBounds(65, 10, 100, 25);
		searchButton.setBounds(180, 10, 100, 25);
		searchComboBox.setBounds(300, 10, 100, 25);
		
		//Add cmponents to Search Panel
		searchPanel.add(searchLabel);
		searchPanel.add(searchTextField);
		searchPanel.add(searchButton);
		searchPanel.add(searchComboBox);
		
		//Prepare Add Panel
		addPanel.setLayout(null);
		addPanel.setBounds(0 , 100, PANEL_WIDTH, 50);
		
		//Set components bounds for Add Panel
		addLabel.setBounds(10, 10, 50, 25);
		addComboBox.setBounds(65, 10, 100, 25);
		addButton.setBounds(180, 10, 100, 25);
		exitButton.setBounds(700, 0, 100, 25);
		
		//Add components to Add Panel
		addPanel.add(addLabel);
		addPanel.add(addButton);
		addPanel.add(addComboBox);
		
		//Prepare Search Results Panel
		searchResultsPanel.setLayout(null);
		searchResultsPanel.setBounds(10, 200, PANEL_WIDTH, 400);	
		
		//Prepare Scroll Pane
		tableScrollPane.setBounds(0, 0, PANEL_WIDTH, 250);
		
		//Prepare Exit Button Panel
		exitButtonPanel.setLayout(null);
		exitButtonPanel.setBounds(10, 500, PANEL_WIDTH, 30);
		exitButtonPanel.add(exitButton);
		
		//Add Panels to Main Panel
		mainPanel.add(searchPanel);
		mainPanel.add(addPanel);
		mainPanel.add(exitButtonPanel);
		mainPanel.add(searchResultsPanel);
		
		addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
					addButtonActionPerformed(evt);
            }
        });
		
		searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
					searchButtonActionPerformed(evt);
            }
        });
		
		exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	exitButtonActionPerformed(evt);
            }
        });
		
		searchTextField.addKeyListener(new java.awt.event.KeyAdapter(){
			public void keyPressed(java.awt.event.KeyEvent evt){
				searchTextFieldKeyPressed(evt);
			}
		});
	}
	
	private void searchTextFieldKeyPressed(java.awt.event.KeyEvent evt){
		if(evt.getKeyChar() == KeyEvent.VK_ENTER)
			searchButton.doClick();
	}
	
	private void exitButtonActionPerformed(java.awt.event.ActionEvent evt){
		System.exit(0);
	}
	
	private void searchButtonActionPerformed(java.awt.event.ActionEvent evt){
		try{
			//Set Table Header and Results based on search Therm
					switch(searchComboBox.getSelectedIndex()){
						case 0:{//Student
							
							CustomTableModel ctm = new CustomTableModel();
							ctm.setType(0);						
							
							Class.forName("com.mysql.jdbc.Driver");
							Connection con = DriverManager.getConnection(UMSDB.getInstance().DATABASE_URL);
													
							String query = "SELECT * FROM student WHERE " +
										   "firstName LIKE '%" + searchTextField.getText() +"%' " +
										   "OR lastName LIKE '%" + searchTextField.getText() +"%' " +
										   "OR cnp LIKE '%" + searchTextField.getText() +"%'";
							
							Statement stmt = con.createStatement();
							ResultSet rs = stmt.executeQuery(query);
							
							while(rs.next()){
								Student stud = new Student();
								
								stud.setFirstName(rs.getString("firstName"));
								stud.setLastName(rs.getString("lastName"));
								stud.setCNP(rs.getString("cnp"));
								stud.setStudentNo(rs.getInt("studentNo"));
								stud.setStatus(rs.getString("status"));
								stud.setFaculty(rs.getInt("facultyId"));
								
								ctm.addRow(stud);
							}
							con.close();
							stmt.close();
							rs.close();
							
							searchTable = new JTable(ctm);
						} break;
						
						case 1:{//Professor
							
							CustomTableModel ctm = new CustomTableModel();
							ctm.setType(1);						
							
							Class.forName("com.mysql.jdbc.Driver");
							Connection con = DriverManager.getConnection(UMSDB.getInstance().DATABASE_URL);
													
							String query = "SELECT * FROM professor WHERE " +
									   	   "firstName LIKE '%" + searchTextField.getText() +"%' " +
									   	   "OR lastName LIKE '%" + searchTextField.getText() +"%' " +
									   	   "OR cnp LIKE '%" + searchTextField.getText() +"%'";							
							Statement stmt = con.createStatement();
							ResultSet rs = stmt.executeQuery(query);
							
							while(rs.next()){
								Professor prof = new Professor();

								prof.setFirstName(rs.getString("firstName"));
								prof.setLastName(rs.getString("lastName"));
								prof.setCNP(rs.getString("cnp"));
								prof.setField(rs.getString("field"));
								prof.setTitle(rs.getString("title"));
								
								ctm.addRow(prof);
							}
							con.close();
							stmt.close();
							rs.close();
							
							searchTable = new JTable(ctm);
						} break;
						
						case 3:{//Course
							
							CustomTableModel ctm = new CustomTableModel();
							ctm.setType(3);						
							
							Class.forName("com.mysql.jdbc.Driver");
							Connection con = DriverManager.getConnection(UMSDB.getInstance().DATABASE_URL);
													
							String query = "SELECT * FROM course WHERE courseName LIKE '%" + searchTextField.getText() +"%'";
							
							Statement stmt = con.createStatement();
							ResultSet rs = stmt.executeQuery(query);
							
							while(rs.next()){
								Course course = new Course();
								
								course.setCourseName(rs.getString("courseName"));
								course.setCourseId(rs.getInt("courseId"));
								course.setSemester(rs.getInt("semester"));
								course.setYearOfStudy(rs.getInt("yearOfStudy"));
								
								if(rs.getString("mandatory").equalsIgnoreCase("Yes"))
									course.setMandatory(true);
								else
									course.setMandatory(false);
								
								ctm.addRow(course);
							}
							con.close();
							stmt.close();
							rs.close();
							
							searchTable = new JTable(ctm);
						} break;
					}
					
			//Add components to Search Result Panel
			tableScrollPane.setViewportView(searchTable);
			searchResultsPanel.add(tableScrollPane);
		}
		catch(Exception e){
			e.printStackTrace();
			JOptionPane pane = new JOptionPane(e.getMessage(), 
					JOptionPane.ERROR_MESSAGE, JOptionPane.DEFAULT_OPTION);
				JDialog dialog = pane.createDialog(null, "Error!");
				dialog.setVisible(true);
		}
	}
	
	private void addButtonActionPerformed(java.awt.event.ActionEvent evt){
		try{
			switch(addComboBox.getSelectedItem().toString()){
				case "Student": 	new AddStudentFrame(this).setVisible(true);
									this.setVisible(false);
									break;
				
				case "Professor": 	new AddProfessorFrame(this).setVisible(true);
									this.setVisible(false);
									break;
									
				case "Course": 		new AddCourseFrame(this).setVisible(true);
									this.setVisible(false);
									break;
				
				case "Group":		new AddGroupFrame(this).setVisible(true);
									this.setVisible(false);
									break;
			}
		}
		catch(Exception e){
			e.printStackTrace();
			JOptionPane pane = new JOptionPane("Exception Found! Check Values!",
					JOptionPane.ERROR_MESSAGE, JOptionPane.DEFAULT_OPTION);
				JDialog dialog = pane.createDialog(null, "Error!");
				dialog.setVisible(true);
		}
	}
}
